package com.yanqu.road.dao.impl.unioncampwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UnionCampWarHoldData;
import com.yanqu.road.entity.unioncampwar.UnionCampWarNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarNoticeDaoImpl extends TempDataObjectDao<UnionCampWarNoticeData> {


    public static String getUnionCampWarNoticeDataSql = "select * from `t_c_union_camp_war_notice` where `weekId`=?";

    public Map<Integer, Map<Integer, List<UnionCampWarNoticeData>>> getUnionCampWarNoticeDataMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<UnionCampWarNoticeData>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getUnionCampWarNoticeDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionCampWarNoticeData data = parse(rs);
                Map<Integer, List<UnionCampWarNoticeData>> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (null == centerGroupMap) {
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                List<UnionCampWarNoticeData> dataList = centerGroupMap.get(data.getGroupId());
                if (dataList == null) {
                    dataList = new ArrayList<>();
                    centerGroupMap.put(data.getGroupId(), dataList);
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean updateData(UnionCampWarNoticeData unionCampWarNoticeData) {
        return false;
    }

    public static String insertUnionCampWarNoticeDataSql = "insert into `t_c_union_camp_war_notice`(centerGroupId,groupId,weekId,attackCampId,attackUserId,defendUnionId,defendUnionName,defendCampId,holdId,winNum,scoreAdd,battleTime) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UnionCampWarNoticeData unionCampWarNoticeData) {
        unionCampWarNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getAttackCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarNoticeData.getAttackUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarNoticeData.getDefendUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarNoticeData.getDefendUnionName()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getDefendCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getHoldId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarNoticeData.getWinNum()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarNoticeData.getScoreAdd()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarNoticeData.getBattleTime()));
        if (getDb().executeNoneQuery(insertUnionCampWarNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionCampWarNoticeData parseData(ResultSet rs) throws SQLException {
        UnionCampWarNoticeData unionCampWarNoticeData = new UnionCampWarNoticeData();
        unionCampWarNoticeData.setCenterGroupId(rs.getInt("centerGroupId"));
        unionCampWarNoticeData.setGroupId(rs.getInt("groupId"));
        unionCampWarNoticeData.setWeekId(rs.getInt("weekId"));
        unionCampWarNoticeData.setAttackCampId(rs.getInt("attackCampId"));
        unionCampWarNoticeData.setAttackUserId(rs.getLong("attackUserId"));
        unionCampWarNoticeData.setDefendUnionId(rs.getString("defendUnionId"));
        unionCampWarNoticeData.setDefendUnionName(rs.getString("defendUnionName"));
        unionCampWarNoticeData.setDefendCampId(rs.getInt("defendCampId"));
        unionCampWarNoticeData.setHoldId(rs.getInt("holdId"));
        unionCampWarNoticeData.setWinNum(rs.getInt("winNum"));
        unionCampWarNoticeData.setScoreAdd(rs.getLong("scoreAdd"));
        unionCampWarNoticeData.setBattleTime(rs.getLong("battleTime"));

        return unionCampWarNoticeData;
    }
}
